package mwt.xml.xdbforms.schemalayer.types;

import java.sql.Date;
import javax.xml.XMLConstants;
import mwt.xml.xdbforms.dblayer.metadata.impl.ColumnMetaData;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;

public class XmlDate extends XmlTypes {

    public XmlDate() {
    }

    public XmlDate(Document doc) {
        super(doc);
    }

    private void buildFragment(ColumnMetaData cmd) {


        Element xtype, restriction, pattern;
        fragment = doc.createDocumentFragment();

        typeName = cmd.getTypeName();
        cmd.setXschemaTypeName(typeName);

        xtype = doc.createElementNS(XMLConstants.W3C_XML_SCHEMA_NS_URI,"xs:simpleType");
        xtype.setAttribute("name", typeName);

        restriction = doc.createElementNS(XMLConstants.W3C_XML_SCHEMA_NS_URI,"xs:restriction");
        restriction.setAttribute("base", "xs:date");

        pattern = doc.createElementNS(XMLConstants.W3C_XML_SCHEMA_NS_URI,"xs:pattern");

        pattern.setAttribute("value", "\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}");

        xtype.appendChild(restriction);
       // restriction.appendChild(pattern);
        fragment.appendChild(xtype);




    }

    /**
     * Richiama le sottoclassi se il tipo
     * e': DATETIME, TIMESTAMP, YEAR, TIME
     * @param cmd
     * @return Frammento del document rappresentante il tipo
     */
    @Override
    public DocumentFragment getSchemaType(ColumnMetaData cmd) {

        typeName = cmd.getTypeName();

        if (!typeName.equals("DATE")) {


            if (typeName.equals("DATETIME")) {

                return new XmlDatetime(doc).getSchemaType(cmd);

            } else {

                if (typeName.equals("TIME")) {

                    return new XmlTime(doc).getSchemaType(cmd);
                } else {


                    if (typeName.equals("TIMESTAMP")) {

                        return new XmlTimeStamp(doc).getSchemaType(cmd);


                    } else {

                        if (typeName.equals("YEAR")) {

                            return new XmlYear(doc).getSchemaType(cmd);

                        } else {

                            return nextType.getSchemaType(cmd);

                        }
                    }
                }
            }

        } else {

            buildFragment(cmd);
            return fragment;

        }



    }

    @Override
    public Object getXSchemaToJavaType(String baseType, String value) {


        /**
         * Errore logico nella clausola if.
         * Nel caso in cui ci deve essere 
         *
         **/

        System.out.println("[XMLDATE] ::-> "+baseType);


        if (!baseType.equals("DATE")) {


            if (baseType.equals("DATETIME")) {

                return new XmlDatetime().getXSchemaToJavaType(baseType, value);

            } else {

                if (baseType.equals("TIME")) {

                    return new XmlTime().getXSchemaToJavaType(baseType, value);
                } else {


                    if (baseType.equals("TIMESTAMP")) {

                        return new XmlTimeStamp().getXSchemaToJavaType(baseType, value);


                    } else {

                        if (baseType.equals("YEAR")) {

                            return new XmlYear().getXSchemaToJavaType(baseType, value);

                        } else {

                            return nextType.getXSchemaToJavaType(baseType, value);

                        }
                    }
                }
            }

        } else {
           Date date = Date.valueOf(value);
           return date;

        }



    }
}

